﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using QLTT.Models;
using QLTT.Commands;
using System.Windows.Input;
using System.Text.RegularExpressions;
using QLTT.Properties;
using System.Windows.Controls;

namespace QLTT.ViewModels
{
    class StudentInvokeModelView:WorkspaceViewModel
    {
        Student _student;
        Class _class;
        Room _room;
        StudentClass _studentClass;
        List<Schedule> _lstSchedule;
        ClassTime _classTime;
        string _sClassDay="";
        string _sFullPaid;
        string _sReason="";
        string _sClassFee;
        string _sMonths;
        string _sInvokeId;
        string _sTransactionDate;
        bool _isInvoice1Expanded=true;
        bool _isInvoice2Expanded;
        icanDataContext _dataContext;
        private DelegateCommand _cancelCommand;
        
        public StudentInvokeModelView(icanDataContext DataContext, Student oStudent, Class oClass, FeePayment oFeePayment, bool isDeposit)
        {
            _dataContext = DataContext;
            _student = oStudent;
            _sFullPaid = oFeePayment.Amount.ToString();
            _sMonths = oFeePayment.Months.ToString();
            _sReason = oFeePayment.Reason;
            _sInvokeId = oFeePayment.ID.ToString();
            _sTransactionDate = oFeePayment.Date.ToString("dd/MM/yyyy",new System.Globalization.CultureInfo("en-US"));
            if (oClass != null)
            {
                _class = oClass;
                _room = _dataContext.Rooms.Where(o => o.ID == _class.Room).Select(o => o).FirstOrDefault();
                _lstSchedule = _dataContext.Schedules.Where(o => o.Class == _class.ID).Select(o => o).ToList();
                if (_lstSchedule.Count > 0)
                {
                    _classTime = _dataContext.ClassTimes.Where(o => o.ID == _lstSchedule[0].ClassTime).Select(o => o).FirstOrDefault();

                }
            }
            base.DisplayName = Properties.Resources.Base_DisplayName_InvoicePrint;
        }
        //Use for repay fee to student
        public StudentInvokeModelView(icanDataContext DataContext, Student oStudent, Class oClass, FeePayment oFeePayment)
        {
            _dataContext = DataContext;
            _student = oStudent;
            _sReason = oFeePayment.Reason;
            _sInvokeId = oFeePayment.ID.ToString();
            _sTransactionDate = oFeePayment.Date.ToString("dd/MM/yyyy",new System.Globalization.CultureInfo("en-US"));
            if (oClass != null)
            {
                _class = oClass;
                _studentClass = _dataContext.StudentClasses.Where(o => o.Student == oFeePayment.Student).Where(o => o.Class == oFeePayment.Class).Select(o => o).FirstOrDefault();
                _sClassFee = _studentClass.Fee.ToString();
                _room = _dataContext.Rooms.Where(o => o.ID == _class.Room).Select(o => o).FirstOrDefault();
                _lstSchedule = _dataContext.Schedules.Where(o => o.Class == _class.ID).Select(o => o).ToList();
                if (_lstSchedule.Count > 0)
                {
                    _classTime = _dataContext.ClassTimes.Where(o => o.ID == _lstSchedule[0].ClassTime).Select(o => o).FirstOrDefault();

                }
            }
            base.DisplayName = Properties.Resources.Base_DisplayName_InvoicePrint;
        }
        public StudentInvokeModelView(icanDataContext DataContext, FeePayment oFeePayment)
        {
            _dataContext = DataContext;
            _sReason = oFeePayment.Reason;
            _sFullPaid = oFeePayment.Amount.ToString();
            _sMonths = oFeePayment.Months.ToString();
            _sInvokeId = oFeePayment.ID.ToString();
            _sTransactionDate = oFeePayment.Date.ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("en-US"));
            Student oStudent = DataContext.Students.Where(o => o.ID == oFeePayment.Student).Select(o => o).FirstOrDefault();
            _student = oStudent;
            Class oClass = DataContext.Classes.Where(o => o.ID == oFeePayment.Class).Select(o => o).FirstOrDefault();
            if (oClass != null)
            {
                _class = oClass;
                _studentClass = _dataContext.StudentClasses.Where(o => o.Student == oFeePayment.Student).Where(o => o.Class == oFeePayment.Class).Select(o => o).FirstOrDefault();
                _sClassFee = _studentClass.Fee.ToString();
                _room = _dataContext.Rooms.Where(o => o.ID == _class.Room).Select(o => o).FirstOrDefault();
                _lstSchedule = _dataContext.Schedules.Where(o => o.Class == _class.ID).Select(o => o).ToList();
                if (_lstSchedule.Count > 0)
                {
                    _classTime = _dataContext.ClassTimes.Where(o => o.ID == _lstSchedule[0].ClassTime).Select(o => o).FirstOrDefault();
                }
            }
            base.DisplayName = Properties.Resources.Base_DisplayName_InvoicePrint;
        }
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new DelegateCommand(Cancel);
                }
                return _cancelCommand;
            }
        }
        private void Cancel()
        {
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
        }
        public string Name
        {
            get {return _student.Name;}
        }
        public string MaHocVien
        {
            get { return _student.ID.ToString(); }
        }
        public Nullable<DateTime> BirthDay
        {
            get
            {
                return _student.DateOfBirth;
            }
        }
        public string Address
        {
            get { return _student.Address; }
        }
        public string ContactInfo
        {
            get { return _student.ContactInfo; }
        }
        public string ClassName
        {
            get { if (_class != null) return _class.Name; else return ""; }
        }
        public string RoomName
        {
            get { if (_room != null) return _room.Name; else return ""; }
        }
        public string ClassDay
        {
            get
            {
                _sClassDay = "";
                string[] dayOfWeeks = { "T2", "T3", "T4", "T5", "T6", "T7", "CN" };
                if (_lstSchedule.Count>0)
                foreach (Schedule oSchedule in _lstSchedule)
                    _sClassDay = _sClassDay + dayOfWeeks[oSchedule.DayOfWeek] + ", ";
                if (_sClassDay != "") _sClassDay = _sClassDay.Substring(0, _sClassDay.Length - 2);
                return _sClassDay;
            }
        }
        public string ClassTime
        {
            get
            {
                if (_classTime != null) return _classTime.StartTime + "-" + _classTime.EndTime;
                else return "";
            }
        }
        public string StartDate
        {
            get
            {
                if (_class != null) return _class.StartDate.ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("en-US"));
                else return "";
            }
        }
        public string EndDate
        {
            get
            {
                if (_class != null) return _class.EndDate.ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("en-US"));
                else return "";
            }
        }
        public string FullPaid
        {
            get { return _sFullPaid; }
        }
        public string ClassFee
        {
            get { return _sClassFee; }
        }
        public string Months
        {
            get { return _sMonths + " tháng"; }
        }
        public string InvokeId
        {
            get { return _sInvokeId; }
        }
        public string Reason
        {
            get { return _sReason; }
        }
        public string PrintInvokeDate
        {
            get 
            {
                return _sTransactionDate;
            }
        }
        public bool IsInvoice1Expanded
        {
            get { return _isInvoice1Expanded; }
            set
            {
                if (_isInvoice1Expanded == value)
                    return;
                _isInvoice1Expanded = value;
                _isInvoice2Expanded = !value;
                base.OnPropertyChanged("IsInvoice1Expanded");
                base.OnPropertyChanged("IsInvoice2Expanded");
            }
        }
        public bool IsInvoice2Expanded
        {
            get { return _isInvoice2Expanded; }
            set
            {
                if (_isInvoice2Expanded == value)
                    return;
                _isInvoice2Expanded = value;
                _isInvoice1Expanded = !value;
                base.OnPropertyChanged("IsInvoice2Expanded");
                base.OnPropertyChanged("IsInvoice1Expanded");
            }
        }
        public void Close()
        {
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
            WorkspaceViewModel ws = this.ParentWorkspace;
            if(ws.GetType() == typeof(StudentViewModel))
                Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, ws);
        }
       
    }
}
